package com.base.caselib.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.base.caselib.R;
import com.base.caselib.bean.CheckBoxBean;
import com.base.commonlib.widget.MuitChooseDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by weikailiang on 2019/10/12.
 */

public class CheckMultipleDropCustom extends FrameLayout{
    private List<CheckBoxBean> mList = new ArrayList<>();
    private String mLastModelIndex = "";
    private TextView mLayout;
    private TextView mTitle;
    private String mRowIndex;
    MuitChooseDialog mDialog;
    private Map<String,String> mMap = new HashMap<>();
    private List<CheckBoxBean> mCholseItem = new ArrayList<>();

    private OnCheckFuOnClick listener;
    public CheckMultipleDropCustom(@NonNull Context context) {
        super(context);
        init(null);
    }

    public CheckMultipleDropCustom(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public CheckMultipleDropCustom(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }
    private void init(AttributeSet attrs){
        LayoutInflater.from(getContext()).inflate(R.layout.item_multipledrop, this);
        mLayout = findViewById(R.id.title_result);
        mTitle = findViewById(R.id.title);
        mLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mDialog!=null){
                    mDialog.show();
                }
            }
        });
    }
    public void createChrid(List<CheckBoxBean> bean,String title){
        if (bean != null && bean.size()>0){
            if (mTitle != null){
                mTitle.setText(title);
            }
            mList.clear();
            mList.addAll(bean);

            mDialog = new MuitChooseDialog(getContext(), "请选择", mList, new MuitChooseDialog.OnItemChooseListener() {
                @Override
                public void onItemChoose(List<Object> o) {
                    mCholseItem.clear();
                    String str = "";
                    for (Object ob : o){
                        mCholseItem.add((CheckBoxBean)ob);
                        str += ((CheckBoxBean) ob).getDropLabel()+",";
                    }
                    if (!TextUtils.isEmpty(str)){
                        mLayout.setText(str.substring(0,str.length()-1));
                    }



                }
            });


//            for (final CheckBoxBean checkBoxBean : bean){
//                CheckBox box = new CheckBox(getContext());
//                box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                    @Override
//                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                        if (listener != null&& mMap!= null) {
//                            if (b) {
//                                listener.onCheckedListener(checkBoxBean,mMap.get(checkBoxBean.getDropValue()),"");
//                            } else {
//                                listener.onUnCheckListener(checkBoxBean,"",mMap.get(checkBoxBean.getDropValue()));
//                            }
//                            //记录这次的索引
//                            mLastModelIndex = mMap.get(checkBoxBean.getDropValue());
//                        }
//                    }
//                });
//                box.setTag(checkBoxBean.getDropValue());
//                box.setText(checkBoxBean.getDropLabel());
//                mLayout.addView(box);
//            }
        }

    }
    public String getmCurrentValue() {

        List<View> views = getAllChildViews(mLayout);

        String str = "";
        for (View view : views){
            if (view instanceof CheckBox){
                CheckBox radioButton = (CheckBox) view;
                if (radioButton.isChecked()){
                    for (CheckBoxBean bean : mList){
                        if (radioButton.getTag().equals(bean.getDropValue())){
                            str += bean.getDropValue()+",";
                        }
                    }
                }
            }
        }

        if (TextUtils.isEmpty(str)){
            return str;
        }else {
            return str.substring(0,str.length()-1);
        }
    }
    private List<View> getAllChildViews(View view) {
        List<View> allchildren = new ArrayList<View>();
        if (view instanceof ViewGroup) {
            ViewGroup vp = (ViewGroup) view;
            for (int i = 0; i < vp.getChildCount(); i++) {
                View viewchild = vp.getChildAt(i);
                allchildren.add(viewchild);
                //再次 调用本身（递归）
                allchildren.addAll(getAllChildViews(viewchild));
            }
        }
        return allchildren;
    }

    public void setMapKeyValue(Map<String,String> map){
        if (map != null&&map.size()>0){
            mMap = map;
        }
    }

    public String getmRowIndex() {
        return mRowIndex;
    }

    public void setmRowIndex(String mRowIndex) {
        this.mRowIndex = mRowIndex;
    }

    public void setOnCheckListener(OnCheckFuOnClick listener){
        this.listener = listener;
    }

    public interface OnCheckFuOnClick{
        void onCheckedListener(CheckBoxBean bean, String modeIndex, String lastModeIndex);
        void onUnCheckListener(CheckBoxBean bean, String modeIndex, String lastModeIndex);
    }


    public void setmCurrentBoxBean(String dropLabel) {
        List<View> views = getAllChildViews(mLayout);
        for (View view : views) {
            if (view instanceof CheckBox) {
                CheckBox checkBox = (CheckBox) view;
                if (dropLabel.equals(checkBox.getText().toString())) {
                    checkBox.setChecked(true);
                }
            }
        }
    }


    /**
     * 获取所有选中 的 tag（dropValue）
     * @return
     */
    public List<String> getCurrentBoxBeanList() {
//        List<View> views = getAllChildViews(mLayout);
        List<String> dropValueList=new ArrayList<>();
//        for (View view : views) {
//            if (view instanceof CheckBox) {
//                CheckBox checkBox = (CheckBox) view;
//                if (checkBox.isChecked()) {
//                    String dropValue = checkBox.getTag().toString();
//                    dropValueList.add(dropValue);
//                }
//            }
//        }
        for (CheckBoxBean checkBoxBean : mCholseItem){
            dropValueList.add(checkBoxBean.getDropValue());
        }
        return dropValueList;
    }


}
